!###############################################
 module linsolver
!###############################################
   implicit none

!  MUMPS parameters:   
   INCLUDE 'mpif.h'
   INCLUDE 'dmumps_struc.h'
   TYPE (DMUMPS_STRUC) mumps_par
   INTEGER IERR

 
!*******************
  contains
!*******************
  
!============================================================================
 subroutine mtrx_idxs(n,col,row,irn,jcn)
! Builds the arrays irn and jcn containing the row and column indices of the 
! non-zero elements of the matrix
!============================================================================
   implicit none
   integer :: i,j
   integer, intent(in) :: n
   integer, dimension(:), intent(in) :: col, row
   integer, dimension(:), intent(out) :: irn, jcn
!============================================================== 

   do i=1,n
     do j=row(i),row(i+1)-1
       irn(j)=i
       jcn(j)=col(j)
     end do
   end do


 end subroutine mtrx_idxs


!=============================================================
 subroutine mtrx_restore(val,col,row,M,n)
! Restore the full matrix from the arrays used to store its
! compressed form
!============================================================== 
   implicit none
   integer :: i,j
   integer, intent(in) :: n
   real(kind=8), dimension(:), intent(in) :: val
   integer, dimension(:), intent(in) :: col,row
   real(kind=8), dimension(:,:), intent(inout) :: M
!============================================================== 

   M=0d0
   do i=1,n
     do j=row(i),row(i+1)-1
       M(i,col(j))=val(j)    
     end do
   end do

 end subroutine mtrx_restore



 end module linsolver

